package com.ns.scs.vsb.service;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ns.core.enums.GeneralEnum;
import com.ns.core.vo.mp.MapGraphVO;
import com.ns.scs.stat.entry.StatEntry;
import com.ns.scs.stat.mapper.StatMapper;
import com.ns.scs.vsb.dto.PanelTagsDto;
import com.ns.scs.vsb.dto.ViewBoardPageDto;
import com.ns.scs.vsb.entry.ViewBoard;
import com.ns.scs.vsb.mapper.ViewBoardMapper;
import com.ns.scs.vsb.vo.PanelTagsVO;
import com.ns.scs.vsb.vo.ViewBoardExtraVO;
import com.ns.scs.vsb.vo.ViewBoardGroupVO;
import com.ns.scs.vsb.vo.ViewBoardVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 图看板模块业务实现接口
 *
 * @author lw
 * @version : v1.0.0
 * @since 2024-11-08
 */
@Service
@RequiredArgsConstructor
public class ViewBoardServiceImpl extends ServiceImpl<ViewBoardMapper, ViewBoard> implements IViewBoardService {

    private final ViewBoardMapper mapper;

    private final StatMapper statMapper;

    @Override
    public ViewBoardGroupVO getViewBoard(String key) {
        ViewBoardGroupVO vbg = new ViewBoardGroupVO();
        List<ViewBoardVO> vbs = mapper.getViewBoard(key);
        // 看板数据
        if (!CollectionUtils.isEmpty(vbs)) {
            List<ViewBoardExtraVO> left = new ArrayList<>();
            List<ViewBoardExtraVO> center = new ArrayList<>();
            List<ViewBoardExtraVO> right = new ArrayList<>();
            ViewBoardExtraVO header = null;
            for (ViewBoardVO vb : vbs) {
                ViewBoardExtraVO extraVO = new ViewBoardExtraVO();
                BeanUtils.copyProperties(vb, extraVO);
                if (StringUtils.pathEquals(extraVO.getLocation(), GeneralEnum.LOCATION.HEADER.getKey())) {
                    header = extraVO;
                } else if (StringUtils.pathEquals(extraVO.getLocation(), GeneralEnum.LOCATION.LEFT.getKey())) {
                    getViewExtraVO(vb, extraVO);
                    left.add(extraVO);
                } else if (StringUtils.pathEquals(vb.getLocation(), GeneralEnum.LOCATION.CENTER.getKey())) {
                    getViewExtraVO(vb, extraVO);
                    center.add(extraVO);
                } else if (StringUtils.pathEquals(vb.getLocation(), GeneralEnum.LOCATION.RIGHT.getKey())) {
                    getViewExtraVO(vb, extraVO);
                    right.add(extraVO);
                }
            }
            vbg.setLeft(left);
            vbg.setCenter(center);
            vbg.setRight(right);
            vbg.setHeader(header);
        }

        // 地图数据
        List<MapGraphVO> graphs = new ArrayList<>();
        // todo mock 数据
        String[] names = {"上海市", "南京市", "四川省", "西藏自治区", "吉林省", "黑龙江省", "台湾省"};
        for (int i = 1; i < 8; i++) {
            MapGraphVO mg = new MapGraphVO();
            mg.setName(names[(i - 1)]);
            mg.setValue(String.valueOf(i * 2000));
            mg.setSelected(1);
            graphs.add(mg);
        }
        vbg.setGraphs(graphs);
        return vbg;
    }

    /**
     * 获取扩展属性
     *
     * @param vb      看板数据
     * @param extraVO 扩展属性
     */
    private void getViewExtraVO(ViewBoardVO vb, ViewBoardExtraVO extraVO) {
        List<PanelTagsVO> panelTagsList = new ArrayList<>();
        // 查询获取面板数据
        if (Objects.isNull(vb.getPanelTags())) {
            return;
        }
        List<PanelTagsDto> panelTags = JSON.parseArray(vb.getPanelTags(), PanelTagsDto.class);
        if (CollectionUtils.isEmpty(panelTags)) {
            return;
        }
        List<Long> params = new ArrayList<>();
        panelTags.forEach(pt -> {
            params.add(pt.getDataId());
        });
        List<StatEntry> statisticsEntries = statMapper.selectBatchIds(params);
        if (CollectionUtils.isEmpty(statisticsEntries)) {
            return;
        }
        for (StatEntry statEntry : statisticsEntries) {
            for (int i = 0; i < panelTags.size(); i++) {
                if (Objects.equals(panelTags.get(i).getDataId(), statEntry.getId())) {
                    PanelTagsVO panelTagsVO = new PanelTagsVO();
                    panelTagsVO.setName(panelTags.get(i).getName());
                    panelTagsVO.setDataId(panelTags.get(i).getDataId());
                    panelTagsVO.setT(statEntry.getCount());
                    panelTagsVO.setV(statEntry.getVariable());
                    panelTagsVO.setSort(i);
                    panelTagsList.add(panelTagsVO);
                }
            }
        }
        extraVO.setPanelTags(panelTagsList);
    }

    @Override
    public Page<ViewBoardVO> getList(ViewBoardPageDto dto) {
        return mapper.getList(dto, new Page<>(dto.getCurrent(), dto.getSize()));
    }
}
